package com.atguign.gulimall.coupon.service.impl;

import com.atguign.gulimall.coupon.dao.SeckillSessionDao;
import com.atguign.gulimall.coupon.dao.SeckillSkuRelationDao;
import com.atguign.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguign.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguign.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationDao seckillSkuRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaysSession() {
        LocalDate now = LocalDate.now();
        LocalDate plusDays = now.plusDays(2);
        LocalDateTime localDateTime = LocalDateTime.of(now, LocalTime.MIDNIGHT);
        LocalDateTime plusDays2 = LocalDateTime.of(plusDays, LocalTime.MAX);
        List<SeckillSessionEntity> seckillSessionEntities = this.baseMapper.selectList(new LambdaQueryWrapper<SeckillSessionEntity>().between(SeckillSessionEntity::getStartTime, localDateTime, plusDays2));
        if(!seckillSessionEntities.isEmpty()){
            seckillSessionEntities.forEach(item->{
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = seckillSkuRelationDao.selectList(new LambdaQueryWrapper<SeckillSkuRelationEntity>().eq(SeckillSkuRelationEntity::getPromotionSessionId, item.getId()));
                item.setRelationSkus(seckillSkuRelationEntities);
            });
            return seckillSessionEntities;
        }
        return null;
    }

}